/*
 * Filename RbcAnallysis.java 2022年08月31日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.service.analysis;

import com.ehome.poct.core.api.constant.enums.ReportAnalysisType;
import com.ehome.poct.core.api.virtual.BldAnalysisService;
import com.ehome.poct.core.api.virtual.ConfAnalysisPermissionService;
import com.ehome.poct.core.api.vo.CheckResultVO;
import com.ehome.poct.core.api.vo.report.ReportAnalysisResultVO;
import com.ehome.poct.core.service.mapping.H5ReportMapping;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * RBC报告分析
 *
 * @author wyc
 * @since 2022/08/31 11:07
 **/
@Service
@RequiredArgsConstructor
public class RbcAnalysis {
    private final BldAnalysisService bldAnalysisService;
    private final ConfAnalysisPermissionService permissionService;
    private final H5ReportMapping reportMapping;

    /**
     * 报告分析
     *
     */
    public ReportAnalysisResultVO analysis(List<CheckResultVO> bldResults, List<CheckResultVO> morphologyResults, String code, Long clinicId) {
        // 没有权限返回空
        if (!permissionService.hasPermission(ReportAnalysisType.RBC.getCode(), clinicId)) {
            return ReportAnalysisResultVO.createEmpty();
        }
        final var analysisContent = reportMapping.to(bldAnalysisService.getRbcByCode(code));
        return Optional.ofNullable(analysisContent).map(it -> {
            // 异常检测数据
            final var order = List.of(8L, 9L, 11L, 12L, 13L);
            final var abnormal = bldResults.stream().filter(
                    r -> order.contains(r.getOrder()) && r.getStatus() == 2).collect(Collectors.toList());
            return new ReportAnalysisResultVO(abnormal, morphologyResults, analysisContent);
        }).orElse(new ReportAnalysisResultVO(null, morphologyResults, null));
    }
}